<template>
  <div class="card form-box">
    <el-form
      ref="materialFormRef"
      :model="formValue"
      :rules="rules"
      label-width="100px"
      size="default"
      :disabled="false"
      label-suffix=" :"
      @submit.enter.prevent="handleSubmit"
      scroll-to-error
    >
      <el-row :gutter="20">
        <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="8">
          <el-form-item label="状态" prop="state" required>
            <el-switch
              v-model="formValue.state"
              :active-value="1017001"
              :inactive-value="1017002"
              active-text="启用"
              inactive-text="禁用"
              inline-prompt
              :active-action-icon="Check"
              :inactive-action-icon="Close"
            />
          </el-form-item>
        </el-col>

        <el-col :sm="12" :md="12" :lg="8" :xl="8">
          <el-form-item label="编码" prop="code" required>
            <el-input v-model="formValue.code" clearable disabled />
          </el-form-item>
        </el-col>
        <el-col :sm="12" :md="12" :lg="8" :xl="8" required>
          <el-form-item label="项目" prop="project">
            <el-input v-model="formValue.project" placeholder="请输入项目" clearable />
          </el-form-item>
        </el-col>
        <el-col :sm="12" :md="12" :lg="8" :xl="8" required>
          <el-form-item label="名称" prop="name">
            <el-input v-model="formValue.name" placeholder="请输入名称" clearable />
          </el-form-item>
        </el-col>
        <el-col :sm="12" :md="12" :lg="8" :xl="8">
          <el-form-item label="零件号" prop="partNum">
            <el-input v-model="formValue.partNum" placeholder="请输入零件号" clearable />
          </el-form-item>
        </el-col>

        <el-col :sm="12" :md="12" :lg="8" :xl="8">
          <el-form-item label="材质" prop="shader">
            <el-input v-model="formValue.shader" placeholder="请输入材质" clearable />
          </el-form-item>
        </el-col>
        <el-col :sm="12" :md="12" :lg="8" :xl="8">
          <el-form-item label="规格" prop="spec">
            <el-input v-model="formValue.spec" placeholder="请输入规格" clearable />
          </el-form-item>
        </el-col>
        <el-col :sm="12" :md="12" :lg="8" :xl="8">
          <el-form-item label="料厚" prop="thick">
            <el-input v-model="formValue.thick" placeholder="请输入料厚" clearable />
          </el-form-item>
        </el-col>
        <el-col :sm="12" :md="12" :lg="8" :xl="8">
          <el-form-item label="重量" prop="weight">
            <el-input v-model="formValue.weight" placeholder="请输入重量" clearable>
              <template #append>
                <el-select v-model="formValue.unit" placeholder="请选择单位" clearable filterable style="width: 115px">
                  <el-option v-for="op in optionsStore.getDictOptions('unit')" :key="op.id" :label="op.codeName" :value="op.id">
                    {{ op.alias }}-{{ op.codeName }}
                  </el-option>
                </el-select>
              </template>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :sm="12" :md="12" :lg="8" :xl="8">
          <el-form-item label="宽度" prop="width">
            <el-input v-model="formValue.width" placeholder="请输入宽度" clearable />
          </el-form-item>
        </el-col>
        <el-col :sm="12" :md="12" :lg="8" :xl="8">
          <el-form-item label="长度" prop="lengths">
            <el-input v-model="formValue.lengths" placeholder="请输入长度" clearable />
          </el-form-item>
        </el-col>
        <el-col :sm="12" :md="12" :lg="8" :xl="8">
          <el-form-item label="步距" prop="stride">
            <el-input v-model="formValue.stride" placeholder="请输入步距" clearable />
          </el-form-item>
        </el-col>
        <el-col :sm="12" :md="12" :lg="8" :xl="8">
          <el-form-item label="单车用量" prop="consumption">
            <el-input v-model="formValue.consumption" placeholder="请输入单车用量" clearable />
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="8">
          <el-form-item label="出件数" prop="outNum">
            <el-input v-model="formValue.outNum" placeholder="请输入名称" clearable />
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="8">
          <el-form-item label="安全库存" prop="safety_stock">
            <el-input v-model="formValue.safety_stock" placeholder="请输入安全库存" clearable />
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="8">
          <el-form-item label="品牌" prop="brand">
            <el-input v-model="formValue.brand" placeholder="请输入品牌" clearable />
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="8">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="formValue.remark" placeholder="请输入备注" clearable type="textarea" />
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="8">
          <el-form-item label="图片" prop="url">
            <UploadImg v-model:image-url="formValue.url" dir="productparts" width="135px" height="135px" border-radius="50%">
              <template #empty>
                <el-icon><Picture /></el-icon>
                <span>请上传产品图片</span>
              </template>
              <template #tip> 图片大小不能超过 3M </template>
            </UploadImg>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="flx-center">
      <el-button type="primary" @click="handleSubmit">保存</el-button>
    </div>
  </div>
</template>

<script setup>
defineOptions({
  name: 'ProductPartsForm'
});
import { ref, nextTick, onMounted } from 'vue';
import { useOptionsStore } from '@/stores/modules/options';
import { ElMessage } from 'element-plus';
import { Check, Close } from '@element-plus/icons-vue';
import { useRoute, useRouter } from 'vue-router';
import { getTimestamp } from '@/utils/index';
import { createMaterial, getMaterialInfo, updateMaterial, getMaterialTypeCascade } from '@/api/modules/basicinfo/material';
import { useTabsStore } from '@/stores/modules/tabs';
import UploadImg from '@/components/Upload/Img.vue';
const optionsStore = useOptionsStore();
const tabStore = useTabsStore();
const route = useRoute();
const router = useRouter();
const materialFormRef = ref();
const cascaderOptions = ref([]);
const formValue = ref({}); //2是产品1是物料
const rules = ref({
  project: [{ required: true, message: '请输入项目', trigger: 'blur' }],
  name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
  partNum: [{ required: true, message: '请输入零件号', trigger: 'blur' }]
});

const handleSubmit = () => {
  materialFormRef.value.validate(async valid => {
    if (!valid) return;
    try {
      const formValueData = { ...formValue.value, type: 2 };
      let { data } =
        !route.query.id || route.query.id === 0 ? await createMaterial(formValueData) : await updateMaterial(formValueData);
      ElMessage.success({ message: `产品保存成功！` });
      if (!route.query.id || route.query.id === 0) {
        router.push(`/basicinfo/productPartsForm?id=${data.id}`);
      } else {
        getInfo();
      }
    } catch (error) {
      console.log(error);
    }
  });
};
const cascaderRef = ref(null);
const handleChange = value => {
  const arr = cascaderRef.value.getCheckedNodes();
  formValue.value.category_name = arr[0].text;
};

const getInfo = async () => {
  const { data } = await getMaterialInfo({ id: route.query.id });
  formValue.value = data;
};

onMounted(async () => {
  const { data } = await getMaterialTypeCascade();
  cascaderOptions.value = data;
  if (!route.query.id || route.query.id === 0) {
    formValue.value = {
      state: 1017001,
      code: `CP${getTimestamp(2)}`
    };
    tabStore.setTabsTitle('新增产品');
  } else {
    getInfo();
    tabStore.setTabsTitle('编辑产品');
  }
});
</script>

<style lang="scss" scoped>
.form-box {
  height: 100%;
}
</style>
